package com.ahnchan.ContactsPlus;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Properties;

import com.ahnchan.ContactsPlus.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class SettingActivity extends Activity {

	protected ContactsPlusSetting setting;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        
        setting = ContactsPlusSetting.instatnce();

        // Set Header color & background color
        setHeader();
        
        // First tab setting radio button
        RadioButton rbContact = (RadioButton) findViewById(R.id.radio_contact_tab);
        RadioButton rbGroup = (RadioButton) findViewById(R.id.radio_group_tab);
        RadioButton rbCompany = (RadioButton) findViewById(R.id.radio_company_tab);
        rbContact.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setting.setFirstTab(setting.TAB_CONTACT);
			}
		});
        rbGroup.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setting.setFirstTab(setting.TAB_GROUP);
			}
		}); 
        rbCompany.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setting.setFirstTab(setting.TAB_COMPANY);
			}
		});
        
        String strFirstTab = setting.getFirstTab();
        if (strFirstTab.equals(setting.TAB_CONTACT)) {
        	rbContact.setChecked(true);
        } else if (strFirstTab.equals(setting.TAB_GROUP)) {
        	rbGroup.setChecked(true);
        } else if (strFirstTab.equals(setting.TAB_COMPANY)) {
        	rbCompany.setChecked(true);
        }
        
        // Display only phone number
        CheckBox ckOnlyPhone = (CheckBox) findViewById(R.id.checkbox_display_only_phone);
        ckOnlyPhone.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setOnlyPhone(true);
                } else {
                    setting.setOnlyPhone(false);
                }
            }
        });
        if (setting.getOnlyPhone()) {
        	ckOnlyPhone.setChecked(true);
        }

        // Sort list by Last name
        CheckBox ckSortLastname = (CheckBox) findViewById(R.id.checkbox_sort_lastname);
        ckSortLastname.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setSortLastname(true);
                } else {
                    setting.setSortLastname(false);
                }
            }
        });
        if (setting.getSortLastname()) {
        	ckSortLastname.setChecked(true);
        }

        // Display Last name
        CheckBox ckDisplayLastname = (CheckBox) findViewById(R.id.checkbox_display_lastname);
        ckDisplayLastname.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setDisplayLastname(true);
                } else {
                    setting.setDisplayLastname(false);
                }
            }
        });
        if (setting.getDisplayLastname()) {
        	ckDisplayLastname.setChecked(true);
        }
        
        // Contact list index available
        CheckBox ckContactIndex = (CheckBox) findViewById(R.id.checkbox_contact_index);
        ckContactIndex.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setContactIndex(true);
                } else {
                    setting.setContactIndex(false);
                }
            }
        });
        if (setting.getContactIndex()) {
        	ckContactIndex.setChecked(true);
        }
        
        // Group list index available
        CheckBox ckGroupIndex = (CheckBox) findViewById(R.id.checkbox_group_index);
        ckGroupIndex.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setGroupIndex(true);
                } else {
                    setting.setGroupIndex(false);
                }
            }
        });
        if (setting.getGroupIndex()) {
        	ckGroupIndex.setChecked(true);
        }

        // Company list index available
        CheckBox ckCompanyIndex = (CheckBox) findViewById(R.id.checkbox_company_index);
        ckCompanyIndex.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setCompanyIndex(true);
                } else {
                    setting.setCompanyIndex(false);
                }
            }
        });
        if (setting.getCompanyIndex()) {
        	ckCompanyIndex.setChecked(true);
        }
        
        // Group list count visible
        CheckBox ckGroupCount = (CheckBox) findViewById(R.id.checkbox_group_count);
        ckGroupCount.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setGroupCount(true);
                } else {
                    setting.setGroupCount(false);
                }
            }
        });
        if (setting.getGroupCount()) {
        	ckGroupCount.setChecked(true);
        }

        // Company list count visible
        CheckBox ckCompanyCount = (CheckBox) findViewById(R.id.checkbox_company_count);
        ckCompanyCount.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setCompanyCount(true);
                } else {
                    setting.setCompanyCount(false);
                }
            }
        });
        if (setting.getCompanyCount()) {
        	ckCompanyCount.setChecked(true);
        }

        // Merge data (Symbol & Number)
        CheckBox ckMerge = (CheckBox) findViewById(R.id.checkbox_merge_symbol);
        ckMerge.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setTypeSymbolNumber(true);
                } else {
                    setting.setTypeSymbolNumber(false);
                }
            }
        });
        if (setting.getTypeSymbolNumber()) {
        	ckMerge.setChecked(true);
        }
        
        // Sorting type
        RadioButton rbType1 = (RadioButton) findViewById(R.id.radio_sort_1);
        RadioButton rbType2 = (RadioButton) findViewById(R.id.radio_sort_2);
        RadioButton rbType3 = (RadioButton) findViewById(R.id.radio_sort_3);
        rbType1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setting.setTypeList(1);
			}
		});
        rbType2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setting.setTypeList(2);
			}
		}); 
        rbType3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setting.setTypeList(3);
			}
		});
        
        int iTypeList = setting.getTypeList();
        if (iTypeList == 1) {
        	rbType1.setChecked(true);
        } else if (iTypeList == 2) {
        	rbType2.setChecked(true);
        } else if (iTypeList == 3) {
        	rbType3.setChecked(true);
        }

        // Spread data (Symbol)
        CheckBox ckSpreadSymbol = (CheckBox) findViewById(R.id.checkbox_spread_symbol);
        ckSpreadSymbol.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setSpreadSymbol(true);
                } else {
                    setting.setSpreadSymbol(false);
                }
            }
        });
        if (setting.getSpreadSymbol()) {
        	ckSpreadSymbol.setChecked(true);
        }

        // Spread data (Number)
        CheckBox ckSpreadNumber = (CheckBox) findViewById(R.id.checkbox_spread_number);
        ckSpreadNumber.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    setting.setSpreadNumber(true);
                } else {
                    setting.setSpreadNumber(false);
                }
            }
        });
        if (setting.getSpreadNumber()) {
        	ckSpreadNumber.setChecked(true);
        }

        //        // Language Setting
//        TextView textLanguage = (TextView)findViewById(R.id.setting_language_data);
//        textLanguage.setText(setting.getLocalLanguage());
    }

    private void setHeader() {

    	// Header back ground color, font color, font size 
    	LinearLayout header1 = (LinearLayout) this.findViewById(R.id.setting_title_bg_1);
        header1.setBackgroundColor(setting.getIndexTitleBGColor());
        TextView title1 = (TextView)this.findViewById(R.id.setting_title_1);
        title1.setTextColor(setting.getIndexTitleColor());
//        title1.setTextSize(setting.getIndexTitleSize());
        
    	LinearLayout header2 = (LinearLayout) this.findViewById(R.id.setting_title_bg_2);
        header2.setBackgroundColor(setting.getIndexTitleBGColor());
        TextView title2 = (TextView)this.findViewById(R.id.setting_title_2);
        title2.setTextColor(setting.getIndexTitleColor());
//        title2.setTextSize(setting.getIndexTitleSize());
        
    	LinearLayout header3 = (LinearLayout) this.findViewById(R.id.setting_title_bg_3);
        header3.setBackgroundColor(setting.getIndexTitleBGColor());
        TextView title3 = (TextView)this.findViewById(R.id.setting_title_3);
        title3.setTextColor(setting.getIndexTitleColor());
//        title3.setTextSize(setting.getIndexTitleSize());
        
    	LinearLayout header4 = (LinearLayout) this.findViewById(R.id.setting_title_bg_4);
        header4.setBackgroundColor(setting.getIndexTitleBGColor());
        TextView title4 = (TextView)this.findViewById(R.id.setting_title_4);
        title4.setTextColor(setting.getIndexTitleColor());
//        title4.setTextSize(setting.getIndexTitleSize());

    	LinearLayout header5 = (LinearLayout) this.findViewById(R.id.setting_title_bg_5);
        header5.setBackgroundColor(setting.getIndexTitleBGColor());
        TextView title5 = (TextView)this.findViewById(R.id.setting_title_5);
        title5.setTextColor(setting.getIndexTitleColor());
//        title5.setTextSize(setting.getIndexTitleSize());

    	LinearLayout header6 = (LinearLayout) this.findViewById(R.id.setting_title_bg_6);
        header6.setBackgroundColor(setting.getIndexTitleBGColor());
        TextView title6 = (TextView)this.findViewById(R.id.setting_title_6);
        title6.setTextColor(setting.getIndexTitleColor());
//        title6.setTextSize(setting.getIndexTitleSize());

        // Display/Sort last name
    	LinearLayout header7 = (LinearLayout) this.findViewById(R.id.setting_title_bg_7);
        header7.setBackgroundColor(setting.getIndexTitleBGColor());
        TextView title7 = (TextView)this.findViewById(R.id.setting_title_7);
        title7.setTextColor(setting.getIndexTitleColor());
//        title7.setTextSize(setting.getIndexTitleSize());
}
    
    private void setText(String message) {
    	TextView text = (TextView)findViewById(R.id.message_text);
    	text.setText(message);
    }
}